//
//  ZZTextView.swift
//  swiftFrame
//
//  Created by hfzhangzhang on 2018/3/22.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

import UIKit

class ZZTextView: UITextView {

}

class UIPlaceholderTextView: UITextView {
    private  var placeholderLable: UILabel?
    var placeholder: String? {
        get{
            return self.placeholder
        }
        set(newValue) {
            if let value = newValue, !value.isEmpty {
                self.placeholderLable = UILabel(frame: CGRect(x: 5, y: 5, width: self.width-10, height: self.height-10))
                self.placeholderLable?.font = self.font
                self.placeholderLable?.text = value
                self.placeholderLable?.textColor = UIColor("999999")
                self.placeholderLable?.customSizeToFit()
                self.addSubview(self.placeholderLable!)
                NotificationCenter.default.addObserver(self, selector:#selector(textViewTextDidChange), name: .UITextViewTextDidChange, object: nil)
            }
        }
    }
    @objc func textViewTextDidChange() -> () {
        if let text = self.text, !text.isEmpty {
            self.placeholderLable?.isHidden = true
        }
        else {
            self.placeholderLable?.isHidden = false
        }
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
